package com.fch.sdlibrary.util;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fch.sdlibrary.R;
import com.fch.sdlibrary.entity.Book;
import com.fch.sdlibrary.global.MyGlobal;

import java.util.List;


public class BookAdapter extends BaseAdapter {
    private List<Book> mData;//定义数据。
    private LayoutInflater mInflater;//定义Inflater,加载我们自定义的布局。

    public BookAdapter(List<Book> mData, LayoutInflater mInflater) {
        this.mData = mData;
        this.mInflater = mInflater;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View viewBook = mInflater.inflate(R.layout.book_item, null);
        //获得学生对象
        if (viewBook != null){
            Book book = mData.get(i);
            //获得自定义布局中每一个控件的对象。
            ImageView img = viewBook.findViewById(R.id.img_book_item);
            TextView name = viewBook.findViewById(R.id.name_book_item);
            TextView author = viewBook.findViewById(R.id.author_book_item);
            TextView quantity = viewBook.findViewById(R.id.quantity_book_item);
            TextView isbn = viewBook.findViewById(R.id.isbn_book_item);
            //将数据一一添加到自定义的布局中。
            Glide.with(img.getContext()).load(MyGlobal.IMG_PATH + book.getBookImg()).into(img);
            name.setText(book.getBookName());
            author.setText(book.getBookAuthor());
            quantity.setText("库存：" + book.getBookQuantity().toString());
            isbn.setText("ISBN：" + book.getBookIsbn());
        }
        return viewBook;
    }
}
